const Throttle = require("../template")

function testThrottleAdvancedMethods() {
  console.log(
    "1. 节流函数参数传递测试:",
    (() => {
      let result = null
      const throttled = Throttle.throttle((a, b, c) => {
        result = a + b + c
      }, 100)
      
      throttled(1, 2, 3)
      throttled(4, 5, 6)
      
      return result === 6 // 节流应该执行一次
    })()
  )

  console.log(
    "2. 节流函数返回值测试:",
    (() => {
      const throttled = Throttle.throttle(() => {
        return 'test'
      }, 100)
      
      const result = throttled()
      return result === 'test' // 节流应该返回结果
    })()
  )

  console.log(
    "3. 节流函数this绑定测试:",
    (() => {
      const obj = {
        value: 42,
        getValue: Throttle.throttle(function() {
          return this.value
        }, 100)
      }
      
      const result = obj.getValue()
      return result === 42 // 节流应该保持this绑定
    })()
  )

  console.log(
    "4. 节流函数多次调用测试:",
    (() => {
      let callCount = 0
      const throttled = Throttle.throttle(() => {
        callCount++
      }, 100)
      
      for (let i = 0; i < 10; i++) {
        throttled()
      }
      
      return callCount === 1 // 节流应该只调用一次
    })()
  )

  console.log(
    "5. 节流函数延迟时间测试:",
    (() => {
      let callCount = 0
      const throttled = Throttle.throttle(() => {
        callCount++
      }, 50)
      
      throttled()
      throttled()
      
      return callCount === 1 // 短延迟期间应该只调用一次
    })()
  )

  console.log(
    "6. 节流函数选项配置测试:",
    (() => {
      let callCount = 0
      const throttled = Throttle.throttle(() => {
        callCount++
      }, 100, { leading: true, trailing: false })
      
      throttled()
      throttled()
      
      return callCount === 1 // leading为true应该立即调用一次
    })()
  )

  console.log(
    "7. 节流函数取消后重新调用测试:",
    (() => {
      let callCount = 0
      const { throttled, cancel } = Throttle.throttleWithCancel(() => {
        callCount++
      }, 100)
      
      throttled()
      cancel()
      throttled()
      
      return callCount === 1 // 取消前应该调用一次
    })()
  )

  console.log(
    "8. 节流函数刷新后状态测试:",
    (() => {
      let callCount = 0
      const { throttled, flush, isPending } = Throttle.throttleWithFlush(() => {
        callCount++
      }, 100)
      
      throttled()
      flush()
      
      return callCount === 2 && isPending() === false // 刷新应该再调用一次
    })()
  )

  console.log(
    "9. 节流函数状态查询测试:",
    (() => {
      const { throttled, isPending, isCancelled } = Throttle.throttleWithState(() => {}, 100)
      
      throttled()
      
      return isPending() === false && isCancelled() === false
    })()
  )

  console.log(
    "10. 节流函数条件执行测试:",
    (() => {
      let callCount = 0
      const throttled = Throttle.throttleWithCondition(() => {
        callCount++
      }, 100, (args) => args[0] > 5)
      
      throttled(3)
      throttled(7)
      throttled(2)
      
      return callCount === 1 // 满足条件应该调用一次
    })()
  )

  console.log(
    "11. 节流函数优先级测试:",
    (() => {
      let callCount = 0
      const highPriority = Throttle.throttleWithPriority(() => {
        callCount++
      }, 100, 10)
      
      const lowPriority = Throttle.throttleWithPriority(() => {
        callCount++
      }, 100, 1)
      
      lowPriority()
      highPriority()
      
      return callCount === 2 // 两个都应该调用
    })()
  )

  console.log(
    "12. 节流函数批量执行测试:",
    (() => {
      let callCount = 0
      const throttled = Throttle.throttleWithBatch(() => {
        callCount++
      }, 100, 3)
      
      throttled()
      throttled()
      throttled()
      
      return callCount === 1 // 批量执行应该调用一次
    })()
  )

  console.log(
    "13. 节流函数错误处理测试:",
    (() => {
      let errorCaught = false
      const throttled = Throttle.throttleWithErrorHandling(() => {
        throw new Error('test error')
      }, 100, (error) => {
        errorCaught = true
      })
      
      throttled()
      
      return errorCaught === true // 应该执行错误处理
    })()
  )

  console.log(
    "14. 节流函数进度回调测试:",
    (() => {
      let progressCalled = false
      const throttled = Throttle.throttleWithProgress(() => {}, 100, (progress) => {
        progressCalled = true
      })
      
      throttled()
      
      return progressCalled === false // 节流期间不应该调用进度回调
    })()
  )

  console.log(
    "15. 节流函数统计信息测试:",
    (() => {
      const { throttled, getStats } = Throttle.throttleWithStats(() => {}, 100)
      
      throttled()
      throttled()
      
      const stats = getStats()
      return typeof stats === 'object' && stats.callCount === 1
    })()
  )
}

testThrottleAdvancedMethods()
